<template>
  <van-cell-group>
    <!--    <van-cell label="描述信息" title="单元格" value="内容" />-->
    <template v-for="(item,index) in commonStore.dateInfoList">

      <van-swipe-cell>
        <template #left>
          <van-button square text="编辑" type="primary" />
        </template>
        <!--        <van-cell :border="false" title="单元格" value="内容" />-->
        <van-cell :label="subscribeText(<dateInfoInterface>item)" :title="item.name"
                  :value="nextYearDate(<dateInfoInterface>item)" />
        <template #right>
          <van-button square text="删除" type="danger" @click="commonStore.deleteDate(index)" />
        </template>
      </van-swipe-cell>

    </template>
  </van-cell-group>
</template>

<script lang="ts" setup>
import { computed } from "vue";
import type { dateInfoInterface } from "@/stores/commonStore";
import { useCommonStore } from "@/stores/commonStore";

import { yellow_people } from "@/assets/ts/people";
import { Lunar, Solar } from "lunar-typescript";

yellow_people.walk();

const commonStore = useCommonStore();


const subscribeText = computed(() => {
  return (item: dateInfoInterface) => {
    if (item.isLunar) {
      return `事件始于农历${item.lunarDate}`;
    }
    return `事件始于公历${item.solarDate}`;
  };
});

// 假设你要转换的农历日期是 2024年农历正月初九
const lunar = Lunar.fromYmd(2024, 1, 9);
const solar = lunar.getSolar();
console.log(`公历日期是：${solar}`);


function getNextLunarBirthday(lunarBirthday: string): string | null {
  if (!lunarBirthday) return null;

  const [birthYear, birthMonth, birthDay] = lunarBirthday.split("/").map(Number);

  const currentDate = new Date();
  // 使用 Solar 类将公历日期转换为农历
  let currentYear = currentDate.getFullYear();
  let currentMonth = currentDate.getMonth() + 1;
  let day = currentDate.getDate();
  console.log("当前公历年" + currentYear, currentMonth, day);

  let currentLunar = Solar.fromYmd(currentYear, currentMonth, day).getLunar();

  let currentLunarYear = currentLunar.getYear();
  let currentLunarMonth = currentLunar.getMonth();
  let currentLunarDay = currentLunar.getDay();
  let currentLunarTimeStamp = new Date(currentLunarYear + "/" + currentLunarMonth + "/"
    + currentLunarDay
  ).getTime();

  // console.log(currentLunar);
  console.log("当前农历年" + currentLunarYear, currentLunarMonth, currentLunarDay);


  console.log(currentLunar);

  // const currentYear = currentDate.getFullYear();

  let nextSolarDate: Date;

  // 检查今年的农历生日对应的公历日期
  let lunar = Lunar.fromYmd(currentLunarYear, birthMonth, birthDay);

  let solarDate = lunar.getSolar();
  let thisYearSolarDate = new Date(solarDate + "");
  console.log(thisYearSolarDate);
  if (thisYearSolarDate.getTime() > currentLunarTimeStamp) {
    // 如果今年的农历生日还没到，就用今年的日期
    nextSolarDate = thisYearSolarDate;
  } else {
    // 否则，计算明年的农历生日对应的公历日期
    lunar = Lunar.fromYmd(currentLunarYear + 1, birthMonth, birthDay);
    nextSolarDate = new Date(lunar.getSolar() + "");
  }

  // 返回格式化的日期字符串
  return `${nextSolarDate.getFullYear()}-${nextSolarDate.getMonth() + 1}-${nextSolarDate.getDate()}`;
}


const nextYearDate = function(item: dateInfoInterface) {

  if (item.isLunar) {//农历
    if (!item.isLunar) return "xxx";
    let dateArr = item.lunarDate?.split("/");
    return `下一次时间${getNextLunarBirthday(item.lunarDate)}`;
  } else {
    if (!item.solarDate) return "xxx";
    return `下一次时间${item.solarDate}`;
  }

};


</script>

<style scoped>

</style>
